package home_8_25.dao.impl;

import com.neu.Dao.BaseDao;
import home_8_25.dao.BusinessDao;

import java.util.List;
import java.util.Map;

public class BusinessDaoImpl extends BaseDao implements BusinessDao {
    @Override
    public Map<String, Object> login(String name, String pwd) {
        Map<String,Object> map = null;

        String sql = "select * from tbusiness where busname=? and buspwd=md5(?)";
        List<Map<String,Object>> list = super.executeQuery(sql,name,pwd);
        if (list.size()>0){
            map = list.get(0);

        }else {
            System.out.println("失败");
        }

        return map;
    }

    @Override
    public List<Map<String, Object>> finAll() {

        List<Map<String,Object>> list = null;
        String sql = "select busid,busname,busaddress,bussprice,bus_deliveryprice,\n" +
                "case\n" +
                "when busstatus=1 then '在'\n" +
                "else '删除'\n" +
                "end business_status\n" +
                "from tbusiness";
        list = super.executeQuery(sql);
        return list;
    }

    @Override
    public List<Map<String, Object>> findByName(String name) {
        List<Map<String,Object>> list = null;
        String sql = "select busid,busname,busaddress,bussprice,bus_deliveryprice,\n" +
                " case\n" +
                " when busstatus=1 then '商家运营中'\n" +
                " else '已打烊'\n" +
                " end business_status\n" +
                " from tbusiness \n" +
                " where business name like ?";
        list = super.executeQuery(sql,"%"+name+"%");
        return list;
    }

    @Override
    public List<Map<String, Object>> findByName2(String name) {
        List<Map<String,Object>> list = null;
        String sql = "select busid,busname,busaddress,bussprice,bus_deliveryprice,busstatus\n" +
                "from tbusiness\n" +
                "where busname = ?";
        list = super.executeQuery(sql,name);
        return list;
    }

    @Override
    public List<Map<String, Object>> statistics() {
        List<Map<String,Object>> list = null;
        String sql = "select busname,count(fname) fnum \n" +
                "from tbusiness tb,tfood tf\n" +
                "where tb.busid = tf.fbusid\n" +
                "group by fbusid\n";
        list = super.executeQuery(sql);
        return list;
    }

    @Override
    public int addBusiness(String name,String id, String address, String explian) {
        int row = 0;
        String sql = "insert into tbusiness (busname,buspwd,busid,busaddress,busexplain)\n" +
                " values (?,md5(8888),?,?,?)\n";
        row = super.executeUpdate(sql,name,id,address,explian);
        return row;
    }

    @Override
    public int editBusiness(String address, String starprice, String deliverprice, String status,String name) {
        int row = 0;

        String sql = "update tbusiness set  busaddress = ? ,bussprice = ? ," +
                " bus_deliveryprice = ? ,busstatus = ? " +
                  " where busname = ?";
        row = super.executeUpdate(sql,address,starprice,deliverprice,status,name);

        return row;
    }
}
